//
//  CustomAlertView.m
//  yunlehui
//
//  Created by saxon on 15/11/17.
//  Copyright © 2015年 saxon. All rights reserved.
//

#import "RTAlertView.h"


@interface RTAlertView ()<UIAlertViewDelegate>

@property (nonatomic, copy) textFieldsBlock textFieldsBlock;

@end

@implementation RTAlertView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles ReturnBlock:(void (^)(int))returnBlock
{
    self = [RTAlertView alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    __weak typeof(self)weakSelf = self;
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if (weakSelf.textFieldsBlock) {
                weakSelf.textFieldsBlock(0, weakSelf.textFields);
            } else if (returnBlock) {
                returnBlock(0);
            }
        }];
        [self addAction:cancel];
        if (otherButtonTitles) {
            
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:otherButtonTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (weakSelf.textFieldsBlock) {
                    weakSelf.textFieldsBlock(1, weakSelf.textFields);
                } else if (returnBlock) {
                    returnBlock(1);
                }
            }];
            [self addAction:confirm];
        }
    
        
    return self;
}
- (void)addTextFieldsBlock:(textFieldsBlock)block
{
    self.textFieldsBlock = block;
}
- (void)show
{
    id vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    dispatch_async(dispatch_get_main_queue(), ^{
        [vc presentViewController:self animated:YES completion:nil];
    });
}
@end
